
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Random;

 public class createAccount extends JFrame{
	 
	 public createAccount(){
		   getContentPane().setBackground(Color.ORANGE);
			getContentPane().add(new Panel());
			setPreferredSize(new Dimension(700, 400));
			pack();
			setVisible(true);
			setTitle("Create Account");
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			}





 private class Panel extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Create the panel.
	 */
	public Panel() {
		setBackground(Color.ORANGE);
		setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(24, 80, 102, 16);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(24, 138, 102, 16);
		add(lblPassword);
		JLabel lblNewLabel_5 = new JLabel("");
		lblNewLabel_5.setIcon(new ImageIcon(LogIn.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		lblNewLabel_5.setBounds(550, 5, 201, 164);
		getContentPane().add(lblNewLabel_5);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setBounds(24, 196, 128, 16);
		add(lblConfirmPassword);
		
		JLabel lblT = new JLabel("Type of User");
		lblT.setBounds(24, 242, 128, 16);
		add(lblT);
		setLayout(null);
				
		textField = new JTextField();
		textField.setBounds(216, 74, 206, 28);
		add(textField);
		textField.setColumns(10);
		
		textField_1 = new JPasswordField();
		textField_1.setColumns(10);
		textField_1.setBounds(216, 132, 206, 28);
		add(textField_1);
		
		textField_2 = new JPasswordField();
		textField_2.setColumns(10);
		textField_2.setBounds(216, 190, 206, 28);
		add(textField_2);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"FACULTY", "STUDENT"}));
		comboBox.setBounds(216, 238, 206, 27);
		add(comboBox);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(175, 310, 117, 29);
		add(btnCancel);	
		btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
            	dispose();
            }
		});
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBackground(Color.YELLOW);
		lblNewLabel.setBounds(0, 0, 450, 60);
		add(lblNewLabel);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(305, 310, 117, 29);
		add(btnRegister);
		btnRegister.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
            	
                boolean reentry = true;;
            	boolean check=textField.getText().matches("^[a-z0-9_-]{3,15}$") && textField_1.getText().matches("^.*(?=.{4,})(?=.*[a-zA-Z])[a-zA-Z0-9]+$") && textField_1.getText().equals(textField_2.getText());
            	
            	if(check){
            	try {
            	
            	 Connection con = Connect.getConnection();
            	 Statement statement =con.createStatement();
            	 String usr = textField.getText();
            	 String pw = textField_1.getText();
            	 String c =comboBox.getSelectedItem().toString();
            	
            	 ResultSet result = statement.executeQuery("SELECT Username FROM STUDENT UNION SELECT Username FROM FACULTY");
            	
            	 while (result.next()) { // process results one row at a time
         	        
         	       if (usr.equals(result.getString(1))){
         	    	  
         	    	   reentry = false;
         	       }
         	      }
            	 
            	 
            	 if (reentry){
            	 result = statement.executeQuery("SELECT ID FROM STUDENT UNION SELECT ID FROM FACULTY");
            	 while (result.next()) { // process results one row at a time
          	        
           	       if (usr.equals(result.getString(1))){
           	    	  
           	    	   reentry = false;
           	       }
           	      }
            	 statement.executeUpdate("INSERT INTO "+ c +"(Username,Password,ID)" + "VALUES('" +usr+"' , '"+ pw + "'" + ",'"+generateID(result)+"'" + ")");
            	 dispose();
            	 if (c.equals("FACULTY")){
            		new FacPersonalInfo(usr);
            	 }
            	 else{
            		 
            		 new StuPersonalInfo(usr);
            	 }
            	 }
            	 else{
            		 JOptionPane.showMessageDialog(null,"Username already in use"); 
            	 }
            	if(!con.isClosed())
            	System.out.println("Successfully connected to " +
            	"MySQL server using TCP/IP...");
            	} catch(Exception eb) {
            		JOptionPane.showMessageDialog(getContentPane(),"Check Connection");
            	} 
            	
            	
            	

              
            
            }
            	else{
            		JOptionPane.showMessageDialog(null,"Ensure that:\nPassword and Confirm Password are the same\nEnsure password is atleast 4 characters and includes at least a number");
            	}
		}
		});
		
		
		
		
		
	}
	int generateID(ResultSet result) throws SQLException{
		char[] ret = new char[9];
		ret[0] ='9';
		Random gen= new Random();
		char[] coll = {'0','1','2','3','4','5','6','7','8','9'};
		for(int i=1;i<9;i++){
			
			ret[i] =  coll[gen.nextInt(10)];
		}
		String re =new String(ret);
		int out =Integer.parseInt(re);
		while (result.next()) { // process results one row at a time
 	        
  	       if (out==result.getInt(1)){
  	    	  return generateID(result);
  	       }
  	      }
	   return out;
	}
}
}
 
